<?php
/**
 * @author 段志岩
 * @date 2008.8.20
 * @summary 处理用户登录相关操作的动作类。
 */

class LoginAction extends Action{
	public function __construct()
	{
		parent::__construct();
	}
	public function init(){}
	public function display(){}
	public function doView(){
		$this->smarty->display("index.tpl");
	}
	public function login()
	{
		$user = new UserModel();

		$signatures = array(
			'email' => array('required' => 'true', 'type'=> 'string'),
			'password' => array('required' => 'true', 'type' => 'string')
			);

		$failureKey = sanitize_vars($this->post, $signatures);

		if($failureKey != "")
		{
			$this->showMessage("请输入<strong>" . $signatures[$failureKey]['name'] . "<strong>" );
		}

		$user->create();
		$user->password = md5($user->email . $user->password);

		$user->addCondition('email', '=', $user->email, 'and');
		$user->addCondition('password', '=', $user->password, '');

		$u = $user->findOne();

		if($u != null)
		{
			$this->session->setAttr('uid', $u['uid']);
			$this->session->setAttr('user', $u);
			$this->forward("Profile");
		}else{
			$this->showMessage("用户名或者密码错误!");
		}
	}
	public function logout()
	{
		$this->session->destroy();
		$this->forward("Index");
	}
	public function showMessage($msg)
	{
		$this->smarty->assign('errorMsg', $msg);
		$this->smarty->display("error.tpl");
	}
};
?>
